﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Report;
using PMPUtils.Goals;
using PMPUtils.Form;
using System.Drawing;

namespace PMP.Report
{
    public partial class DailyUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {
                txtSprint.Text = Request.Params["SPRINT"];

                RefreshGridView();
            }
        }

        private void RefreshGridView()
        {
            // Final Reviews
            DataTable dtFinalReviews = DailyUpdateOperator.ReadFinalReviews(txtSprint.Text.Trim());

            grvFinalReviews.DataSource = dtFinalReviews.DefaultView;
            grvFinalReviews.DataBind();

            // Need More Informtion
            DataTable dtNMI = DailyUpdateOperator.ReadNMI(txtSprint.Text.Trim());

            grvNMI.DataSource = dtNMI.DefaultView;
            grvNMI.DataBind();

            // Peer Reviews
            DataTable dtPeerReviews = DailyUpdateOperator.ReadPeerReview(txtSprint.Text.Trim());

            grvPeerReviews.DataSource = dtPeerReviews.DefaultView;
            grvPeerReviews.DataBind();

            // Reopens
            DataTable dtReopens = DailyUpdateOperator.ReadReopen(txtSprint.Text.Trim());

            grvReopens.DataSource = dtReopens.DefaultView;
            grvReopens.DataBind();

            FormatGridViews();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshGridView();
        }

        private void FormatGridViews()
        {
            FormatFinalReviews();
            FormatNMI();
            FormatPeerReviews();
            FormatReopens();
        }

        private void FormatPeerReviews()
        {
            // Status
            foreach (GridViewRow row in grvPeerReviews.Rows)
            {
                // Priority
                int priorityCell = GridViewOperator.GetCellID(grvPeerReviews, "StoryPriority");
                String priority = GridViewOperator.GetCellStringValue(grvPeerReviews, row, "StoryPriority");

                if (priority.Equals("HIGH"))
                {
                    row.Cells[priorityCell].BackColor = Color.Red;
                }
                else if (priority.Equals("MED"))
                {
                    row.Cells[priorityCell].BackColor = Color.Orange;
                }
            }
        }

        private void FormatReopens()
        {
            // Status
            foreach (GridViewRow row in grvReopens.Rows)
            {
                // Priority
                int priorityCell = GridViewOperator.GetCellID(grvReopens, "StoryPriority");
                String priority = GridViewOperator.GetCellStringValue(grvReopens, row, "StoryPriority");

                if (priority.Equals("HIGH"))
                {
                    row.Cells[priorityCell].BackColor = Color.Red;
                }
                else if (priority.Equals("MED"))
                {
                    row.Cells[priorityCell].BackColor = Color.Orange;
                }
            }
        }

        private void FormatFinalReviews()
        {
            // Status
            foreach (GridViewRow row in grvFinalReviews.Rows)
            {
                // Days
                int daysCellID = GridViewOperator.GetCellID(grvFinalReviews, "In Review(Days)");
                int days = GridViewOperator.GetCellIntValue(grvFinalReviews, row, "In Review(Days)");

                if (days >= 5)
                {
                    row.Cells[daysCellID].BackColor = Color.Red;
                }
                else if (days >= 3)
                {
                    row.Cells[daysCellID].BackColor = Color.Orange;
                }

                // Priority
                int priorityCell = GridViewOperator.GetCellID(grvFinalReviews, "Priority");
                String priority = GridViewOperator.GetCellStringValue(grvFinalReviews, row, "Priority");

                if (priority.Equals("HIGH"))
                {
                    row.Cells[priorityCell].BackColor = Color.Red;
                }
                else if (priority.Equals("MED"))
                {
                    row.Cells[priorityCell].BackColor = Color.Orange;
                }
            }
        }

        private void FormatNMI()
        {
            // Status
            foreach (GridViewRow row in grvNMI.Rows)
            {
                // Days
                int daysCellID = GridViewOperator.GetCellID(grvNMI, "In Need More Info(Days)");
                int days = GridViewOperator.GetCellIntValue(grvNMI, row, "In Need More Info(Days)");

                if (days >= 5)
                {
                    row.Cells[daysCellID].BackColor = Color.Red;
                }
                else if (days >= 3)
                {
                    row.Cells[daysCellID].BackColor = Color.Orange;
                }

                // Priority
                int priorityCell = GridViewOperator.GetCellID(grvNMI, "Priority");
                String priority = GridViewOperator.GetCellStringValue(grvNMI, row, "Priority");

                if (priority.Equals("HIGH"))
                {
                    row.Cells[priorityCell].BackColor = Color.Red;
                }
                else if (priority.Equals("MED"))
                {
                    row.Cells[priorityCell].BackColor = Color.Orange;
                }
            }
        }
    }
}
